﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing;
/// <summary>
///图片处理
/// </summary>
public class Obj
{
        /// <summary>
        /// 文件删除---存在则删除
        /// </summary>
        /// <param name="file_path">传入文件路径</param>
        public static void file_delete(string file_path)
        {
            if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(file_path)))
            {
                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(file_path));
            }
        }

        /// <summary>
        /// 载取指定长度的字符串
        /// </summary>
        /// <param name="Str">要载取的字符串</param>
        /// <param name="startIdex">从第几个字符开始</param>
        /// <param name="length">载取的长度</param>
        /// <returns>载取到的字符串</returns>
        public static string subStr(string Str, int startIdex, int length)
        {
            int Itmp = Str.Length - startIdex;
            if (Str.Length > length)
            {
                return Str.Substring(startIdex, length) + "....";
            }
            else
            {
                return Str.Substring(startIdex);
            }
        }
        /// <summary>
        /// 载取指定长度的字符串 除去css样式
        /// </summary>
        /// <param name="oldStr">要载取的字符串</param>
        /// <param name="maxLength">从第几个字符开始</param>
        /// <param name="endWith">载取的长度</param>
        /// <returns>载取到的字符串</returns>
        public static string StringTruncat(string oldStr, int maxLength, string endWith)
        {
            string strTmp = "";
            if (!string.IsNullOrEmpty(oldStr))
            {
                strTmp = Regex.Replace(oldStr, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                strTmp = strTmp.Replace("<", "").Replace(">", "").Replace("&nbsp;", "");
                strTmp = strTmp.Replace("FONT", "").Replace("style", "").Replace("face", "").Replace("div", "");
                strTmp = strTmp.Replace("\r\n", "").Replace("<p>", "").Replace("</p>", "");
                if (strTmp.Length > maxLength)
                {
                    strTmp = strTmp.Substring(0, maxLength) + endWith;
                }
            }
            return strTmp;
        }
        public static string subStr2(string Str, int startIdex, int length)
        {
            int Itmp = Str.Length - startIdex;
            if (Str.Length >= length)
            {
                return Str.Substring(startIdex, length);
            }
            else
            {
                return Str.Substring(startIdex);
            }
        }

        /// <summary>
        /// 下载的方法
        /// </summary>
        /// <param name="FileName"></param>
        public static void FileDownload(string FileName)
        {
            String FullFileName = System.Web.HttpContext.Current.Server.MapPath(FileName);
            FileInfo DownloadFile = new FileInfo(FullFileName);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ClearHeaders();
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }


        /// <summary>
        /// 图片高清晰缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路经</param>
        /// <param name="thumbnailPath">新图路经</param>
        /// <param name="targetSize">缩放大小</param>
        public static void MakeThumbnail(System.Web.HttpPostedFile originalImagePath, string thumbnailPath, int targetSize)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalImagePath.InputStream, true);

            Size newSize = new Size();
            if (originalImage.Width > targetSize || originalImage.Height > targetSize)
            {
                if (originalImage.Height > originalImage.Width)
                {
                    newSize.Width = (int)(originalImage.Width * ((float)targetSize / (float)originalImage.Height));
                    newSize.Height = targetSize;
                }
                else
                {
                    newSize.Width = targetSize;
                    newSize.Height = (int)(originalImage.Height * ((float)targetSize / (float)originalImage.Width));
                }
            }
            else
            {
                newSize.Width = originalImage.Width;
                newSize.Height = originalImage.Height;
            }



            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(newSize.Width, newSize.Height);

            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, newSize.Width, newSize.Height),
                new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height),
                System.Drawing.GraphicsUnit.Pixel);

            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }
        #region  曹志平的公共方法

        /// <summary>
        /// 截取指定长度的字符串及显示形式
        /// </summary>
        /// <param name="strString">需要截取的字符串</param>
        /// <param name="length">需要截取的长度</param>
        /// <param name="display">省略显示的形式。如“……”，“......”</param>
        /// <returns>截取到的字符串</returns>
        public static string strSubString(string strString, int length, string display)
        {
            if (strString.Length > length)
            {
                return strString.Substring(0, length) + display;
            }
            else
            {
                return strString;
            }
        }
        #endregion
}